//
//  StoreCollectViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/28.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "StoreCollectCell.h"
#import "CommonCollectModel.h"

#import "StoreDetailViewController.h"

#import "StoreHotelDetailViewController.h"

#import "StoreCollectViewController.h"

@interface StoreCollectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) NSMutableArray <CommonCollectModel *>*dataArray;

@end

@implementation StoreCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self refreshParam];
    [self request_myCollect];
}

- (void)refreshParam {
    self.pageSize = 20;
    self.pageNum = 1;
}

- (void)initView {
    
    [self.view addSubview:self.tableview];
    
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.dataArray[indexPath.row].seller.classId isEqualToString:Store_Category_Hotel]) {
        return 190;
    } else {
        
        if (self.dataArray[indexPath.row].sellerGoods.count == 0) {
            return 190;
        } else {
            return 260;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreCollectCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    WEAKSELF
    cell.StoreCollectCellBlock = ^{
        [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.dataArray[indexPath.section].seller.sellerId];

    };
    
    cell.StoreCollectPackageClickBlock = ^(NSInteger index) {
      
        CommonPackageModel *model = weakSelf.dataArray[indexPath.row].sellerGoods[index];
        [[TT_JumpManager sharedManager] jumpWithDetailModel:model completion:^(BOOL success, NSString * _Nonnull message) {}];

    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.dataArray[indexPath.row].seller.classId isEqualToString:Store_Category_Hotel]) {
        StoreHotelDetailViewController *vc = [[StoreHotelDetailViewController alloc] init];
        vc.storeId = self.dataArray[indexPath.row].seller.sellerId;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        StoreDetailViewController *vc = [[StoreDetailViewController alloc] init];
        vc.storeId = self.dataArray[indexPath.row].seller.sellerId;
        [self.navigationController pushViewController:vc animated:YES];
    }
    

}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"StoreCollectCell" bundle:nil] forCellReuseIdentifier:@"StoreCollectCellID"];
    }
    return _tableview;
}

- (NSMutableArray<CommonCollectModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_myCollect {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSMutableDictionary *t = @{}.mutableCopy;

    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];

    [t setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [t setValue:@(1) forKey:@"collectType"];//商家

    [dic setValue:t forKey:@"t"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MyCollect];

    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];
                
                if ([list isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        CommonCollectModel *model = [CommonCollectModel modelWithDictionary:listDic];

                        NSDictionary *seller = [listDic valueForKey:@"seller"];
                        NSDictionary *sellerQualityAuthentication = [listDic valueForKey:@"sellerQualityAuthentication"];
                        
                        NSArray *sellerGoods = [listDic valueForKey:@"sellerGoods"];
                        NSArray *sellerOpusList = [listDic valueForKey:@"sellerOpusList"];
                        
                        model.seller = [CommonSellerModule modelWithDictionary:seller];
                        
                        if ([sellerQualityAuthentication isKindOfClass:[NSDictionary class]]) {
                            model.sellerQualityAuthentication = [CommonQualityTagModel modelWithDictionary:sellerQualityAuthentication];
                        }
                        
                        if ([sellerGoods isKindOfClass:[NSArray class]]) {
                            model.sellerGoods = @[].mutableCopy;
                            for (int j = 0; j < sellerGoods.count; j++) {
                                NSDictionary *sellerGoodsDic = sellerGoods[j];
                                CommonPackageModel *goodsModel = [CommonPackageModel modelWithJSON:sellerGoodsDic];
                                
                                [model.sellerGoods addObject:goodsModel];
                            }
                        }
                        
                        if ([sellerOpusList isKindOfClass:[NSArray class]]) {
                            model.sellerOpusList = @[].mutableCopy;
                            
                            for (int j = 0; j < sellerOpusList.count; j++) {
                                NSDictionary *sellerOpusListDic = sellerOpusList[j];
                                CommonOpusModel *opusModel = [CommonOpusModel modelWithDictionary:sellerOpusListDic];
                                [model.sellerOpusList addObject:opusModel];
                            }
                        }
                        [weakSelf.dataArray addObject:model];
                    }
                    [weakSelf.tableview reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

@end
